package com.mfcxy.serviceImpl;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.mfcxy.entity.User;
import com.mfcxy.entityvo.SearchUser;
import com.mfcxy.mapper.IUserMapper;
import com.mfcxy.service.IUserService;
import com.mfcxy.tools.CheckClassAttributes;

/**
 * user 业务逻辑
 * 
 * @author mfcxy
 *
 */
@Service
public class UserService extends BaseService implements IUserService {

	// 注入了Iusermapper
	@Autowired
	private IUserMapper usermapper;
	
	@Autowired
	private CheckClassAttributes check;
	
	@Autowired(required = false)
	private HttpSession session;

	/**
	 * 新增业务逻辑
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public int save(User user) {
		//对密码进行2次md5 加密
		String password = getTools().passwordEncryption(user.getPassword());
		user.setPassword(password);
		return usermapper.save(user);
	}

	/**
	 * 查询用户
	 * 
	 * @return
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.mfcxy.service.IUserService#selctList()
	 */
	@Override
	public Map<String, Object> selctList(int p) {
		// 查询总记录数
		int count = usermapper.getCount();
		// 返回给控制器的map数据
		Map<String, Object> map = new HashMap<>();

		// 传递给usermapper sql参数
		Map<String, Object> mapparam = new HashMap<>();

		// 计算每页的开始记录数
		mapparam.put("first", getPage().getFirst(p));
		// 每页的记录数量
		mapparam.put("rowlist", getPage().getTotalRecords());

		// 用户数据
		map.put("userlist", usermapper.selectList(mapparam));
		// 当前分页数
		map.put("p", p);
		// 总页数
		map.put("page", getPage().getTotalPage(count));
		// 返回给控制器
		return map;
	}

	@Override
	public Map<String, Object> searchList(int p, SearchUser su) {
		
		//根据条件查询总记录数
		Map<String, Object> countMap = new HashMap<>();
		
		//通过反射获取对象的数据
		countMap = check.getObjVal(su);
		
		System.out.println(countMap);
		
		// 查询总记录数
		int count = usermapper.getCountWhere(countMap);
		
		
		// 返回给控制器的map数据
		Map<String, Object> map = new HashMap<>();

		// 传递给usermapper sql参数
		Map<String, Object> mapparam = new HashMap<>();
		
		//查询条件
		mapparam = countMap;

		// 计算每页的开始记录数
		mapparam.put("first", getPage().getFirst(p));
		// 每页的记录数量
		mapparam.put("rowlist", getPage().getTotalRecords());
		
		// 用户数据
		map.put("userlist", usermapper.searchList(mapparam));
		// 当前分页数
		map.put("p", p);
		// 总页数
		map.put("page", getPage().getTotalPage(count));
		// 返回给控制器
		return map;
	}

	/**
	 * 根据id删除管理员
	 * @param id
	 * @return
	 */
	@Override
	public int delById(int id) {
		// TODO Auto-generated method stub
		return usermapper.delById(id);
	}

	
	/**
	 * 批量删除管理员根据id
	 * @param ids
	 * @return
	 */
	@Override
	public int delsByIds(String ids) {
		// TODO Auto-generated method stub
		
		List<String> list = java.util.Arrays.asList(ids.split(",")); 
		
		return usermapper.delsByIds(list);
	}

	/**
	 *  检查用户名是否重复
	 * @param username
	 * @return
	 */
	@Override
	public boolean checkUserName(String username) {
		// TODO Auto-generated method stub
		User u = usermapper.findUserByUseerName(username);
		if(u != null) {
			return true;
		} else {
			return false;
		}
	}

	
	/**
	 * 根据id查询用户
	 * @param id
	 * @return
	 */
	@Override
	public User findUserById(int id) {
		// TODO Auto-generated method stub
		return usermapper.findUserById(id);
	}

	/**
	 * 查询非自己的用户名是否被使用
	 */
	@Override
	public boolean checkUserName(Map<String, Object> map) {
		// TODO Auto-generated method stub
		User u = usermapper.checkUserName(map);
		if(u != null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@Override
	public int updateUserById(User user) {
		// TODO Auto-generated method stub
		return usermapper.updateUserById(user);
	}

	/**
	 * 重置密码
	 * @param password
	 * @param id
	 * @return
	 */
	@Override
	public int restpw(String password, int id) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		map.put("password", getTools().passwordEncryption(password));
		map.put("id", id);
		return usermapper.restpw(map);
	}

	/**
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public boolean Login(User user) {
		// TODO Auto-generated method stub
		User findUser = usermapper.findUserByUseerName(user.getUsername());
		//是否能查询到用户
		if(findUser != null) {
			String password = getTools().passwordEncryption(user.getPassword());
			//查询到用户验证密码是否正确
			if(findUser.getPassword().equals(password)) {
				//存储到sessoin中
				session.setAttribute("user", findUser);
				
				//查询用户拥有的权限
				List<Map<String , Object>> plist = usermapper.selectUserPermissions(findUser.getId());
				
				List<String> listsession = new ArrayList<>();
				
				for ( Map<String , Object> map : plist) {
					if(map.get("codeorurl") != null) {
						listsession.add(map.get("codeorurl").toString());
					}
				}
				//权限存储到session中
				session.setAttribute("permissions", listsession);
				
				return true;
			}
		}
		return false;
	}

	@Override
	public String toString() {
		return "UserService [usermapper=" + usermapper + ", check=" + check + "]";
	}
	

}
